package ch.usi.da;

import ch.usi.da.paxos.PaxosProtocol;
import ch.usi.da.tcp.Client;
import ch.usi.da.tcp.Server;
import ch.usi.da.udp.CustomUDPClient;
import ch.usi.da.udp.CustomUDPServer;
import sun.security.krb5.internal.UDPClient;

import javax.print.attribute.standard.DateTimeAtCompleted;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: Sergey Karakovskiy
 * Date: Nov 7, 2009
 * Time: 8:13:24 PM
 */
public class Main
{
    public static void main(String[] args) throws IOException, UnknownHostException
    {
        // some tests:
        if (args.length > 0)
        {
            if ("-server".equals(args[0]))
            {
                //
                CustomUDPServer customUDPServer = new CustomUDPServer();
                customUDPServer.startServer(4224);

            }
            else if ("-client".equals(args[0]))
            {
//                CustomUDPClient customUDPClient = new CustomUDPClient();
//                customUDPClient.act();
                
                UDPClient udpClient = new UDPClient("localhost", 4224);
                udpClient.send("message".getBytes());
                byte[] message = udpClient.receive();
                Date d = new Date();
                d.getDate();
                System.out.println(new Date() + "new String(message) = " + new String(message));

            }
            else
            {
                System.out.println("Unrecognized args! Quitting...");
            }
        }
        else
        {
            System.err.println("Warning: args are empty");
        }

//        System.out.println("Paxos starts here:");
//        PaxosProtocol paxosProtocol = new PaxosProtocol();
//        paxosProtocol.propose(54, 21);
//        paxosProtocol.decide(54, 42);
        ConfigReader configReader = new ConfigReader();
        if (configReader.getProcessRole() == ProcessRole.PROPOSER)
        {
            //create proposer
        }

//        if ("server".equals(args[0] == null ? "" : args[0]))
//        {
//            Server server = new Server(4242);
//        }else
//        {
//            Client client = new Client();
//            client.run(4242);
//        }

        /*
           local values
           PID-SEQ
              does not receive enough replies, it waits;
              //after timeout; repeat the phase 1a message with higher ballot number

        */
    }
}
